package com.itqf.controller;


import com.itqf.entity.User;
import com.itqf.service.UserService;
import com.itqf.service.impl.UserServiceImpl;
import com.itqf.utils.Constants;
import com.itqf.utils.RandomUtils;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;


@WebServlet("/user")
public class UserController extends BaseServlet {

    public String check(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String username = req.getParameter("username");
        if (username == null) {
            return Constants.HAS_USER;
        }
        UserService userService= new UserServiceImpl();
        boolean flag =userService.checkedUser(username);
        if(flag){
            return Constants.HAS_USER;
        }
        return Constants.NOT_HAS_USER;
    }


    public String register(HttpServletRequest req, HttpServletResponse resp)  {
        Map<String,String[]> parameterMap = req.getParameterMap();
        String userpassword = req.getParameter("userpassword");
        User user  =new User();
        try {
            BeanUtils.populate(user,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        user.setStatus(Constants.USER_ACTIVE);
        user.setOccupation(Constants.CUSTOMER);
        user.setUcode(RandomUtils.createActive());
        user.setUserpassword(userpassword);
        UserService userService = new UserServiceImpl();
        try {
            userService.registerUser(user);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            req.setAttribute("registerMsg","注册失败！");
            return Constants.FOW+"/register.jsp";
        }
        return Constants.FOW+"/login.jsp";
    }
    public String login(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        String username = req.getParameter("username");
        String userpassword = req.getParameter("userpassword");
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String codeStr = (String)session.getAttribute("code");
        if(code==null||!code.equalsIgnoreCase(codeStr)){
            req.setAttribute("msg","验证码错误！");
            return Constants.FOW+"/login.jsp";
        }
        UserService userService = new UserServiceImpl();
        User user = userService.login(username, userpassword);
        if (user == null) {
            req.setAttribute("msg","账号或者密码错误");
            return Constants.FOW+"/login.jsp";
        }
        session.setAttribute("loginUser",user);

        return Constants.FOW+"/index.jsp";
    }



    public String logOut(HttpServletRequest req, HttpServletResponse resp) throws SQLException {
        HttpSession session = req.getSession();
        session.removeAttribute("loginUser");
        req.setAttribute("msg","注销登录成功！");
        return Constants.FOW+"/login.jsp";
    }
}
